{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e2fcfe87-2f41-4533-a616-1c8c11ce446a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def is_palindrome_couplet(s: str) -> bool:\n",
    "    # 移除字符串中的空格\n",
    "    s = s.replace(\" \", \"\")\n",
    "    # 判断字符串是否与其倒序字符串相等\n",
    "    return s == s[::-1]\n",
    "\n",
    "# 测试\n",
    "test_str = \"上海自来水来自海上\"\n",
    "is_palindrome_couplet(test_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d45da810-7271-486e-8ee8-264f4a0fca5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def is_palindrome_couplet(s: str) -> bool:\n",
    "    # 移除字符串中的空格\n",
    "    s = s.replace(\" \", \"\")\n",
    "    # 判断字符串是否与其倒序字符串相等\n",
    "    return s == s[::-1]\n",
    "\n",
    "# 测试\n",
    "test_str = \"上海自来的水来自海上\"\n",
    "is_palindrome_couplet(test_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "10a1dc76-1873-42d9-8083-f73a6c53c9e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5.5, 6, 7, 8, 9, 10)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def process_numbers(*args):\n",
    "    # 计算平均值\n",
    "    average = sum(args) / len(args)\n",
    "    # 找出所有大于平均值的实数\n",
    "    greater_than_average = [num for num in args if num > average]\n",
    "    # 返回元组\n",
    "    return (average, *greater_than_average)\n",
    "# 测试\n",
    "test_numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n",
    "process_numbers(*test_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bf7fde47-8e1f-47b1-aad0-420b51f649b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(5, 23), (11, 17), (17, 11), (23, 5)]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def is_prime(n):\n",
    "    \"\"\"检查一个数字是否是素数\"\"\"\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    if n <= 3:\n",
    "        return True\n",
    "    if n % 2 == 0 or n % 3 == 0:\n",
    "        return False\n",
    "    i = 5\n",
    "    while i * i <= n:\n",
    "        if n % i == 0 or n % (i + 2) == 0:\n",
    "            return False\n",
    "        i += 6\n",
    "    return True\n",
    "\n",
    "def goldbach_pairs(even_number):\n",
    "    \"\"\"找到所有总和为给定偶数的素数对.\"\"\"\n",
    "    if even_number % 2 != 0 or even_number < 2:\n",
    "        return \"输入必须是正偶数.\"\n",
    "\n",
    "    pairs = []\n",
    "    for i in range(2, even_number):\n",
    "        if is_prime(i) and is_prime(even_number - i):\n",
    "            pairs.append((i, even_number - i))\n",
    "    return pairs\n",
    "# 测试\n",
    "test_number = 28\n",
    "goldbach_pairs(test_number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9c4563a3-56b4-4cbc-9446-90b26bff93b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 1, 3, 4, 7, 7, 8]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def pivot_list(x, n):\n",
    "    \"\"\"对列表重新排序，使所有小于 n 的元素都位于 n 之前，所有大于 n 的元素都位于 n 之后\"\"\"\n",
    "    less_than_n = [i for i in x if i < n]\n",
    "    equal_to_n = [i for i in x if i == n]\n",
    "    greater_than_n = [i for i in x if i > n]\n",
    "    \n",
    "    return less_than_n + equal_to_n + greater_than_n\n",
    "# 测试\n",
    "test_list = [5, 7, 1, 8, 3, 7, 4]\n",
    "pivot_value = 7\n",
    "pivot_list(test_list, pivot_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7e452b08-3d6e-43ab-ba8d-2cc529ff019a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义一个简单的函数，将输入的整数加1\n",
    "def add_one(x):\n",
    "    return x + 1\n",
    "# 测试列表\n",
    "test_list = [1, 2, 3, 4, 5]\n",
    "\n",
    "# 使用map函数将add_one应用于test_list中的每个元素\n",
    "result = list(map(add_one, test_list))\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b9a16330-c722-4c96-a800-b4bd2c1dcb45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from functools import reduce\n",
    "\n",
    "# 定义一个函数，将两个输入的整数相乘\n",
    "def multiply(x, y):\n",
    "    return x * y\n",
    "# 测试列表\n",
    "test_list = [1, 2, 3, 4, 5]\n",
    "\n",
    "# 使用reduce函数将multiply应用于test_list中的所有元素\n",
    "result = reduce(multiply, test_list)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d4428bfe-efe7-4e5c-b3b9-38009eb48445",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6, 8, 10]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义一个函数，检查一个数是否为偶数\n",
    "def is_even(n):\n",
    "    return n % 2 == 0\n",
    "\n",
    "# 测试列表\n",
    "test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "# 使用filter函数筛选出列表中的偶数\n",
    "even_numbers = list(filter(is_even, test_list))\n",
    "even_numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ee16c0ef-a58b-47c6-957f-281b835a22e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Alice', 'B'), ('Bob', 'A'), ('Charlie', 'C'), ('David', 'B')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义一个函数，根据成绩返回等级\n",
    "def get_grade(score):\n",
    "    if score >= 90:\n",
    "        return 'A'\n",
    "    elif score >= 80:\n",
    "        return 'B'\n",
    "    elif score >= 70:\n",
    "        return 'C'\n",
    "    elif score >= 60:\n",
    "        return 'D'\n",
    "    else:\n",
    "        return 'F'\n",
    "\n",
    "# 给定的学生列表\n",
    "students = [('Alice', 85), ('Bob', 92), ('Charlie', 78), ('David', 88)]\n",
    "\n",
    "# 使用map函数将成绩转换为等级\n",
    "students_with_grades = list(map(lambda student: (student[0], get_grade(student[1])), students))\n",
    "students_with_grades"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a6293b6e-df6e-4bb8-be9e-1cd1316a8828",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from functools import reduce\n",
    "\n",
    "# 定义一个字符串列表\n",
    "strings = [\"hello\", \"world\", \"python\", \"programming\"]\n",
    "\n",
    "# 使用reduce函数计算所有字符串的长度之和\n",
    "total_length = reduce(lambda acc, s: acc + len(s), strings, 0)\n",
    "total_length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d7469c8b-1430-4992-90c5-425450b0bff8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(65.0, ['APPLE', 'BANANA', 'CHERRY', 'DATE', 'ELDERBERRY'])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义一个包含商品名称和价格的列表\n",
    "products = [\n",
    "    {\"name\": \"apple\", \"price\": 40},\n",
    "    {\"name\": \"banana\", \"price\": 10},\n",
    "    {\"name\": \"cherry\", \"price\": 70},\n",
    "    {\"name\": \"date\", \"price\": 60},\n",
    "    {\"name\": \"elderberry\", \"price\": 20}\n",
    "]\n",
    "\n",
    "# 过滤出价格高于50元的商品，并计算平均价格\n",
    "higher_than_50 = [p for p in products if p[\"price\"] > 50]\n",
    "average_price = sum(p[\"price\"] for p in higher_than_50) / len(higher_than_50) if higher_than_50 else 0\n",
    "\n",
    "# 将商品名称转换为大写\n",
    "uppercase_names = [p[\"name\"].upper() for p in products]\n",
    "\n",
    "average_price, uppercase_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ce3154b-a6b2-45c5-aac3-87071504ba80",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
